package com.qingxin.qrCode.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.Crops;
import com.qingxin.park.entity.CropsType;
import com.qingxin.park.entity.Cropsjg;
import com.qingxin.park.entity.CropsjgType;
import com.qingxin.park.entity.Cropsyz;
import com.qingxin.park.entity.CropsyzType;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.service.CompanyService;
import com.qingxin.park.service.CropsService;
import com.qingxin.park.service.CropsjgService;
import com.qingxin.park.service.CropsyzService;
import com.qingxin.park.service.FarmParkService;
import com.qingxin.qrCode.entity.QRCode;
import com.qingxin.qrCode.entity.QrNum;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;
/**
 * 二维码
 * @author Administrator
 *
 */
@Service
public class QRCodeService extends BaseService<QRCode, String> {
	
	@Autowired
	private CompanyService companyService;
	
	@Autowired
	private CropsService cropsService;
	
	@Autowired
	private CropsyzService cropsyzService;
	
	@Autowired
	private CropsjgService cropsjgService;
	
	@Autowired
	private FarmParkService farmParkService;
	
	@Autowired
	private QrNumService qrNumService;
	/**
	 * 二维码列表数据
	 * @param companytype
	 * @param companyid
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String leftdata, String type,PageParam pageParam, LoginUser loginUser){
		if("0".equals(leftdata) && StringUtils.isNotEmpty(leftdata)){
			Querier querier = Querier.create("from QRCode where deltype = 0 and type="+type);
			querier.append(pageParam.getOrderby(" order by createTime asc"));
			Pager pager = pageByQuerier(querier, pageParam);
			return pager;
		}
		Querier querier = Querier.create("from QRCode where deltype = 0 and parkid=:parkid and type ="+type);
		querier.setParam("parkid", leftdata);
		querier.append(pageParam.getOrderby(" order by createTime asc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	public Pager qrnumlist(String codeid, PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("from QrNum where codeid=:codeid and deltype = 0 ");
		querier.setParam("codeid", codeid);
		querier.append(pageParam.getOrderby(" order by is_activation desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	/**
	 * 添加
	 * @param qrCode
	 * @param number
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(QRCode qrCode,LoginUser loginUser, HttpServletRequest request){
		Date now = new Date();
		qrCode.setCreateTime(now);
		qrCode.setCreateBy(loginUser.getLogin_name());
		FarmPark farmPark = this.farmParkService.get(qrCode.getParkid());
		String companyid = farmPark.getCompanyid();
		String parkName = farmPark.getParkName();
		String companyNo = companyService.get(companyid).getCompanyNo();
		String companyName = companyService.get(companyid).getCompanyName();
		String parkNo = farmParkService.get(qrCode.getParkid()).getParkNo();
		String type = qrCode.getType();
		String cropNo = "";
		String cropName = "";
		switch (type) {
		case "1":
			cropNo = cropsService.get(qrCode.getCropid()).getCropCode();
			cropName = cropsService.get(qrCode.getCropid()).getCropName();
			break;
		case "2":
			cropNo = cropsyzService.get(qrCode.getCropid()).getCropCode();
			cropName = cropsyzService.get(qrCode.getCropid()).getCropName();
			break;
		case "3":
			cropNo = cropsjgService.get(qrCode.getCropid()).getCropCode();
			cropName = cropsjgService.get(qrCode.getCropid()).getCropName();
			break;
		default:
			break;
		}
		
		String codeString = (String) getByHql("select code from QrNum where companyid=? and parkid=? and cropid=? order by code desc", companyid, qrCode.getParkid(), qrCode.getCropid());
		int index = 1;
		if(codeString != null){ 
			index = Integer.parseInt(codeString.substring(8,13));
			index++;
		}
		String startCode = qrCode.getType() + companyNo + parkNo + cropNo + String.format("%05d", index);
		int codeInt1 = 0;
		for(int j= 0;j<startCode.length()-1;j++){
			codeInt1 += Integer.parseInt(startCode.charAt(j)+"");
		}
		String jiaoyan1 = String.valueOf(codeInt1 % 10);
		startCode = startCode + jiaoyan1;
		
		String endCode = qrCode.getType() + companyNo + parkNo + cropNo + String.format("%05d", index+qrCode.getShu() - 1);
		int codeInt2 = 0;
		for(int j= 0;j<endCode.length()-1;j++){
			codeInt2 += Integer.parseInt(endCode.charAt(j)+"");
		}
		String jiaoyan2 = String.valueOf(codeInt2 % 10);
		endCode = endCode + jiaoyan2;
		
		
		QRCode qrCode2 = new QRCode(null, companyid, qrCode.getParkid(), qrCode.getCropid(),qrCode.getCreateBy() ,qrCode.getCreateTime(), qrCode.getShu(), parkNo,companyNo, cropNo, "0",qrCode.getType(),parkName,companyName,cropName,"","");
		qrCode2.setQrcodeStart(startCode);
		qrCode2.setQrcodeEnd(endCode);
		this.save(qrCode2);
		
		for(int i=1,n=qrCode.getShu();i<=n;i++){
			String code = qrCode.getType() + companyNo + parkNo + cropNo + String.format("%05d", index);
			int codeInt = 0;
			for(int j= 0;j<code.length();j++){
				codeInt += Integer.parseInt(code.charAt(j)+"");
			}
			String jiaoyan = String.valueOf(codeInt % 10);
			code = code + jiaoyan;
			index ++;
			QrNum qrNum = new QrNum(null, qrCode2.getCodeid(),code,companyid, qrCode.getParkid(), qrCode.getCropid(),loginUser.getLogin_name() ,now,"0","0");
			this.qrNumService.save(qrNum);
		}
		return null;
	}
	

	/**
	 * 删除
	 * @param codeid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public Object delete(String codeid, LoginUser loginUser, HttpServletRequest request){
		Object obj = getByHql("select qrnumid from QrNum where codeid=? and is_activation = 1", codeid);
		if(obj != null){
			return new SysError("此批二维码下有二维码已被使用");
		}
		QRCode old = getSafeEntity(codeid);
		QRCode qrcode = new QRCode();
		qrcode.setDeltype("1");
		String fields = "deltype";
		BeanUtil.setTargetFields(old, qrcode, fields, true);
		Updater<QRCode> updater = new Updater<>(old, fields);
		this.update(updater);
		return null;
	}
	
	
	/**
	 * 获得所有公司
	 * @param loginUser
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Company> getParkData(LoginUser loginUser){
		List<Company> list = listByHql("select new com.qingxin.park.entity.Company(companyid,companyName)" +
				" from Company");
		if(list == null){
			return new ArrayList<Company>();
		}
		return list;
	}
	
	/**
	 * 获取菜单树数据
	 * @param parkid
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TreeNode> getUITreeDataType(String parkid,String type, boolean isParent){
		List<TreeNode> list = new ArrayList<TreeNode>();
		switch (type) {
		case "1":
			StringBuilder hql1 = new StringBuilder();
			hql1.append("select new com.qingxin.park.entity.CropsType(p.typeid, p.typeName)");
			hql1.append(" from CropsType p where p.pid='0'");
			hql1.append(" order by p.typeName asc");
			List<CropsType> types = listByHql(hql1.toString());
			if(types != null){
				for(CropsType t : types){
					list.add(new TreeNode(t.getTypeid(), t.getTypeName(), "TYPE", null, isParent));
				}
			}
			break;
		case "2":
			StringBuilder hql2 = new StringBuilder();
			hql2.append("select new com.qingxin.park.entity.CropsyzType(p.typeid, p.typeName)");
			hql2.append(" from CropsyzType p where p.pid='0'");
			hql2.append(" order by p.typeName asc");
			List<CropsyzType> types2 = listByHql(hql2.toString());
			if(types2 != null){
				for(CropsyzType t : types2){
					list.add(new TreeNode(t.getTypeid(), t.getTypeName(), "TYPE", null, isParent));
				}
			}
			break;
		case "3":
			StringBuilder hql3 = new StringBuilder();
			hql3.append("select new com.qingxin.park.entity.CropsjgType(p.typeid, p.typeName)");
			hql3.append(" from CropsjgType p where p.pid='0'");
			hql3.append(" order by p.typeName asc");
			List<CropsjgType> types3 = listByHql(hql3.toString());
			if(types3 != null){
				for(CropsjgType t : types3){
					list.add(new TreeNode(t.getTypeid(), t.getTypeName(), "TYPE", null, isParent));
				}
			}
			break;
		default:
			break;
		}
		
		return list;
	}
	
	/**
	 * 获取作物数据
	 * @param parkid
	 * @param typeid
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TreeNode> getUITreeDataProduct(String parkid,String type1, String type, boolean isParent){
		List<TreeNode> list = new ArrayList<TreeNode>();
		switch (type1) {
		case "1":
			StringBuilder hql = new StringBuilder();
			hql.append("select new com.qingxin.park.entity.Crops(c.cropid, c.cropName)");
			hql.append(" from Crops c where c.cropsType.typeid=?");
			hql.append(" order by c.cropName asc");
			List<Crops> crops = listByHql(hql.toString(), type);
			if(crops != null){
				for(Crops c : crops){
					list.add(new TreeNode(c.getCropid(), c.getCropName(), "CROP", null, isParent));
				}
			}
			break;
		case "2":
			StringBuilder hql2 = new StringBuilder();
			hql2.append("select new com.qingxin.park.entity.Cropsyz(c.cropid, c.cropName)");
			hql2.append(" from Cropsyz c where c.cropsyzType.typeid=?");
			hql2.append(" order by c.cropName asc");
			List<Cropsyz> crops2 = listByHql(hql2.toString(), type);
			if(crops2 != null){
				for(Cropsyz c : crops2){
					list.add(new TreeNode(c.getCropid(), c.getCropName(), "CROP", null, isParent));
				}
			}
			break;
		case "3":
			StringBuilder hql3 = new StringBuilder();
			hql3.append("select new com.qingxin.park.entity.Cropsjg(c.cropid, c.cropName)");
			hql3.append(" from Cropsjg c where c.cropsjgType.typeid=?");
			hql3.append(" order by c.cropName asc");
			List<Cropsjg> crops3 = listByHql(hql3.toString(), type);
			if(crops3 != null){
				for(Cropsjg c : crops3){
					list.add(new TreeNode(c.getCropid(), c.getCropName(), "CROP", null, isParent));
				}
			}
			break;
		default:
			break;
		}
		
		return list;
	}
	
	
}
